/**
 * Created by iwm on 2017/1/23.
 */
var pictureUrl = '';
$(document).ready(function () {

    initUploadAvatar();

    addUser();
    modifyUser();
    //$("#avatar").fileinput("upload");
    $('#myModal').on('hidden.bs.modal', function (e) {
        // do something...
        //console.log('===========');
        $('#userinfo input').val("");
        //$("#avatraUrl").attr('src',"http://manshe-app.oss-cn-beijing.aliyuncs.com/default_avatar.jpg");
        $(".kv-avatar").find("img").attr('src', "http://manshe-app.oss-cn-beijing.aliyuncs.com/default_avatar.jpg")
    })
    return true;

});

function clearModal() {

}
//uploadUrl: "/common/uploadimage",
//src="http://manshe-app.oss-cn-beijing.aliyuncs.com/default_avatar.jpg"
function initUploadAvatar() {
    $("#avatar").fileinput({
        uploadUrl: "/common/uploadimage",
        overwriteInitial: true,
        showUpload: false,
        uploadAsync: true,
        maxFileSize: 1500,
        showClose: false,
        showCaption: false,
        browseLabel: '',
        removeLabel: '',
        browseIcon: '<i class="glyphicon glyphicon-folder-open"></i>',
        removeIcon: '<i class="glyphicon glyphicon-remove"></i>',
        removeTitle: 'Cancel or reset changes',
        elErrorContainer: '#kv-avatar-errors-1',
        msgErrorClass: 'alert alert-block alert-danger',
        defaultPreviewContent: '<img src="http://manshe-app.oss-cn-beijing.aliyuncs.com/default_avatar.jpg" alt="Your Avatar" style="width:160px">',
        layoutTemplates: {main2: '{preview} ' + ' {remove} {browse}'},
        allowedFileExtensions: ["jpg", "png", "gif"]
    }).on('fileuploaded', function (event, data, id, index) {
        var ret = data.response;
        if (0 != ret.errno) {
            toastr.error('头像上传失败', '', {positionClass: 'toast-bottom-center'});
        } else {
            pictureUrl = ret.data.url;
            console.log(pictureUrl);
        }

    }).on("filebatchselected", function (event, files) {
        // trigger upload method immediately after files are selected
        $("#avatar").fileinput("upload");
    });
}
function addUser() {
    $("#adduser").click(function () {
        $('#myModal').modal("show");
    });
    $("#confirm").click(function () {
        //console.log('=========')
        var myData = new FormData();
        if ('' != $('#userid').val()) {
            myData.append('user_id', $('#userid').val());
        }
        myData.append('user_name', $('#username').val());

        myData.append('nick_name', $('#nickname').val());
        myData.append('phone', $('#phone').val());
        myData.append('flag', $('#flag').val());
        myData.append('age', $('#age').val());
        myData.append('sex', $('#sex').val());
        myData.append('constellation', $('#constellation').val());
        myData.append('dating_intention', $('#dating_intention').val());
        myData.append('marriage_status', $('#marriage_status').val());
        myData.append('tag', $('#tag').val());
        myData.append('location', $('#location').val());
        myData.append('introduction', $('#introduction').val());
        myData.append('sexual_orientation', $('#sexual_orientation').val());
        myData.append('avatar', pictureUrl);

        $.ajax({
            url: '/api/user/adduser',
            timeout: 100000,
            type: 'post',
            dataType: 'json',
            contentType: false,
            processData: false,
            data: myData,
            success: function (rs) {
                if (rs.errno != 0) {
                    toastr.error(rs.errmsg, '', {positionClass: 'toast-bottom-center'});
                } else {
                    toastr.success(rs.errmsg, '', {positionClass: 'toast-bottom-center'});
                    $('#myModal').modal("hide");
                    //window.location.reload();
                }

            },
            error: function () {
                toastr.error('系统错误', '', {positionClass: 'toast-bottom-center'});
            }
        });
    });

}
function modifyUser() {
    $(".caozuo").each(function () {
        var id = $(this).eq(0).find("button")[0].id;
        //console.log(id);
        //return false;
        $("#" + id).click(function () {
            $('#userid').val(id);
            $.ajax({
                url: '/api/user/userdetail',
                timeout: 100000,
                type: 'get',
                dataType: 'json',
                data: {
                    'user_id': id
                },
                success: function (rs) {
                    if (rs.errno != 0) {
                        toastr.error(rs.errmsg, '', {positionClass: 'toast-bottom-center'});
                    }
                    fillModal(rs.data);
                    $('#myModal').modal("show");
                },
                error: function () {
                    toastr.error('系统错误', '', {positionClass: 'toast-bottom-center'});
                }
            });
        });
    });
}

function fillModal(data) {
    //console.log(data);
    //console.log(data.avatar);
    $("#username").val(data.id);
    $("#username").val(data.user_name);
    $("#nickname").val(data.nick_name);
    $("#phone").val(data.phone);
    console.log(data.flag);
    var flag = "option[text=" + data.flag + "]";
    $("#flag").find(flag).attr("selected", true)
    $("#age").val(data.age);
    $("#sex").val(data.sex);
    $("#constellation").val(data.constellation);
    $("#marriage_status").val(data.marriage_status);
    $("#sexual_orientation").val(data.sexual_orientation);
    $("#dating_intention").val(data.dating_intention);
    $("#home").val(data.home);
    $("#location").val(data.location);
    $("#tag").val(data.tag);
    $("#introduction").val(data.introduction);
    if ('' == data.avatar) {
        console.log('头像信息为空');
    } else {
        //console.log(data.avatar);
        $(".kv-avatar").find("img").attr('src', data.avatar);
    }
}
/*
 {
 'user_id': $('#userid').val(),
 'user_name': $('#username').val(),
 'nick_name': $('#nickname').val(),
 'phone': $('#phone').val(),
 'flag': $('#flag').val(),
 'age': $('#age').val(),
 'sex': $('#sex').val(),
 'constellation': $('#constellation').val(),
 'dating_intention': $('#dating_intention').val(),
 'marriage_status': $('#marriage_status').val(),
 'tag': $('#tag').val(),
 'location': $('#location').val(),
 'introduction': $('#introduction').val(),
 'sexual_orientation': $('#sexual_orientation').val(),
 'home': $('#home').val(),
 'avatar': pictureUrl,
 }
 */
